package com.chinaoryx;

import org.apache.log4j.Logger;
import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;
import org.mozilla.intl.chardet.nsPSMDetector;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.ByteOrderMarkDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;

public class MyTest {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MyTest.class);
	
	CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); // A singleton.

	
	boolean found = false;
	String result;
	int lang;
	
	
	public MyTest() {
		// Add the implementations of info.monitorenter.cpdetector.io.ICodepageDetector: 
	    // This one is quick if we deal with unicode codepages: 
	    detector.add(new ByteOrderMarkDetector()); 
	    // The first instance delegated to tries to detect the meta charset attribut in html pages.
//	    detector.add(new ParsingDetector(true)); // be verbose about parsing.
	    // This one does the tricks of exclusion and frequency detection, if first implementation is 
	    // unsuccessful:
//	    detector.add(UnicodeDetector.getInstance());
	    detector.add(JChardetFacade.getInstance()); // Another singleton.
	    detector.add(ASCIIDetector.getInstance()); // Fallback
	}



	public static void main(String[] args) throws MalformedURLException, IOException {
		MyTest obj = new MyTest();
		
		Charset charset = null;
		
		File dir = new File("/home/hyman/Havi/Design/DB/Stored Procedure/RecycleItem/");
		
		
		File[] files = dir.listFiles();
		for (File file : files) {
			
			if (!file.getName().equals("SP_RCY_ITEM_CALC_BY_RT.sql"))
			{
				continue;
			}
			charset = obj.detector.detectCodepage(file.toURL());
			
			logger.info(file.toURI() + ":" + charset);
			
			
			CharsetDetector charDect = new CharsetDetector();
	        URL url = file.toURL();
	        String[] probableSet = charDect.detectAllCharset(url.openStream());
	        for (String a : probableSet)
	        {
	            logger.info(a);
	        }
		}
		//public InputStreamReader(InputStream in, CharsetDecoder dec) {
		
//	    charset = obj.detector.detectCodepage(document.toURL());
		
		
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("/home/hyman/Havi/Design/DB/Stored Procedure/RecycleItem/SP_RCY_ITEM_CALC_BY_RT.sql"), "windows-1252"));
		String str = bufferedReader.readLine();
		while (str != null) {
//			String abc = new String(str.getBytes(), "UTF-8");
			System.out.println(str);
			str = bufferedReader.readLine();
		}
		bufferedReader.close();
		
	}

}
